package pe.com.bbva.efcap.servidor.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		System.out.println("Iniciando...");
//		CtConGrecPe21Rq ctConGrecPe21Rq = new CtConGrecPe21Rq();
//		CtBodyRq ctBodyRq = new CtBodyRq();
//		ctBodyRq.setCodigoCentral("");
//		ctBodyRq.setNumeroDocumentoIdentidad("2");
//		ctBodyRq.setTipoDocumentoIdentidad("R");
//		ctConGrecPe21Rq.setData(ctBodyRq);
//		CtHeaderRq ctHeaderRq = new CtHeaderRq();
//		ctHeaderRq.setOpcionAplicacion("0");
//		ctHeaderRq.setTerminalContable("d");
//		ctHeaderRq.setTerminalLogico("dd");
//		ctHeaderRq.setUsuario("Usu");
//		ctConGrecPe21Rq.setHeader(ctHeaderRq);
//		try {
//			System.out.println("Llamando WS...");
//			CtConGrecPe21Rs rs = new PE21ServiceImpl().getGRECPE21PortTypeImplPort().callPE21(ctConGrecPe21Rq);
//			System.out.println(rs.getData().getCodigoCentral());
//			System.out.println(rs.getData().getNombreEmpresa());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println("00-12-2007".substring(6));
//		System.out.println("2007-12".substring(0,4));
////		System.out.println("CADENA: "+anidarColumnaACatalogoEnFormula("X", "BG", "ABGT888+SI(BGT69=0,0,(RAT115/(BG69+BG60))*100)+BBBG10BG"));
//		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//		RatingService ratingService = (RatingService)ctx.getBean("ratingServiceImpl");
//		List<PFRatingBean> list = ratingService.obtenerListaRating("");
//		Calendar calendar = Calendar.getInstance();
//		Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
//		for(PFRatingBean r : list){
//			Rating rating = new Rating();
//			rating.setCalificacion(r.getCalificacionRating());
//			rating.setCodigoCentralCliente(r.getCodigoCentral());
//			rating.setCuenta(r.getCodigoCuenta());
//			rating.setDescripcionCuenta(r.getDescripcionCuenta());
//			rating.setEstado("A");
//			rating.setFechaCreacion(timestamp);
//			rating.setInflacion(new BigDecimal(r.getInflacion()));
//			rating.setMesEjercicio(new BigDecimal(r.getMesesEjercicio()));
//			rating.setMonto(new BigDecimal(r.getMonto()));
//			rating.setNombreEmpresa(r.getNombreEmpresa());
//			rating.setPeriodo(r.getFechaPeriodoCalculado());
//			rating.setTipoEstadoFinanciero(r.getTipoEstadoFinanciero());
//			ratingService.registrarRating(rating);
//		}
		
		List<Bitacora> lista = new ArrayList<Bitacora>();
		lista.add(new Bitacora("S1", "T1", "O1", "R1"));
		lista.add(new Bitacora("S1", "T1", "O1", "R2"));
		lista.add(new Bitacora("S1", "T1", "O2", "R3"));
		lista.add(new Bitacora("S1", "T1", "O2", "R4"));
		lista.add(new Bitacora("S1", "T2", "O3", "R5"));
		lista.add(new Bitacora("S1", "T2", "O3", "R6"));
		lista.add(new Bitacora("S1", "T2", "O4", "R7"));
		lista.add(new Bitacora("S1", "T2", "O4", "R8"));
		lista.add(new Bitacora("S2", "T3", "O5", "R9"));
		lista.add(new Bitacora("S2", "T3", "O5", "R10"));
		lista.add(new Bitacora("S2", "T3", "O6", "R11"));
		lista.add(new Bitacora("S2", "T3", "O6", "R12"));
		lista.add(new Bitacora("S2", "T4", "O7", "R13"));
		lista.add(new Bitacora("S2", "T4", "O7", "R14"));
		lista.add(new Bitacora("S2", "T4", "O8", "R15"));
		lista.add(new Bitacora("S2", "T4", "O8", "R16"));
		lista.add(new Bitacora("S1", "T2", "O8", "R16"));
		
		Map<String, Map<String, Map<String, List<String>>>> map = new HashMap();
		for(Bitacora b : lista){
			Map<String, Map<String, List<String>>> ms = map.get(b.getSistema());
			if(ms == null){
				ms = new HashMap();				
			}
			Map<String, List<String>> mt = ms.get(b.getTerritorio());
			if(mt == null){
				mt = new HashMap();
			}
			List<String> mo = mt.get(b.getOficina());
			if(mo == null){
				mo = new ArrayList();
			}
			mo.add(b.getRegistro());
			mt.put(b.getOficina(), mo);
			ms.put(b.getTerritorio(), mt);
			map.put(b.getSistema(), ms);
		}
		
		System.out.println(map);
		
		for(String s : map.keySet()){
			System.out.println(s);
			for(String t : map.get(s).keySet()){
				System.out.println(" "+t);
				for(String o : map.get(s).get(t).keySet()){
					System.out.println("  "+o);
					for(String r : map.get(s).get(t).get(o)){
						System.out.println("   "+r);
					}
				}
			}			
		}
		
	}
	

	public static String anidarColumnaACatalogoEnFormula(String columna, String catalogo, String formula){
		int pcat = 0; //indica de posicion para la cadena catalogo		
		String cadena = "";
		String cadDer = formula;
		pcat = cadDer.indexOf(catalogo);
		while (pcat != -1){
			String cadIzq = cadDer.substring(0, pcat + catalogo.length());
			cadDer = cadDer.substring(pcat + catalogo.length());
			if((cadIzq.length() == catalogo.length() 
					|| !Character.isLetter(cadIzq.charAt(cadIzq.length()-catalogo.length()-1)) ) 
					&& (cadDer.length() > 0 && Character.isDigit(cadDer.charAt(0)))){
				cadIzq = cadIzq.replace(catalogo, columna+catalogo);
			}
			cadena+=cadIzq;
			pcat = cadDer.indexOf(catalogo);
		}
		cadena+=cadDer;
		return cadena;
	}

}
